<?php

namespace Common\Logic;

use Think\Controller;

/**
 * Description of DealerApplyLogic.class控制器实例
 * 
 * @author zhangzhen
 * @date 2017-1-4
 */
class DealerApplyLogic extends Controller {

    /**
     * 经销商申请开通服务
     * @modify zhangzhen 2017-01-04
     * @param integer $applyId     申请ID
     * @param string $userName     账号名称
     * @param string $mobile       手机号
     * @param string $enddate      到期日期
     * @param integer $idcard      证件号
     * @param integer $idcardpath  证件照
     
     */
    public function handle($applyId, $userName, $mobile, $enddate, $idcard, $idcardpath, $areacode = '86') {
        //检查用户是否已注册
        
        $dealerLoginModel = D('DealerLogin');
        
        $dealerLogin = $dealerLoginModel->getInfoByMobile($mobile, $areacode);
        if ($dealerLogin) {
            returnJson(403, 123, '账号已存在');
        }
        //开启事务处理
        $dealerApplyModel = D('DealerApply');
        $dealerApplyModel->startTrans();
        $commit = true;
  
        //更新经销商申请状态
        if ($commit) {
            $result = $dealerApplyModel->setStatus($applyId, 1);
            if ($result == false) {
                $commit = false;
            }
        }
        $password = getRandomPassword();
        //添加主账号
        $result = $userId = $dealerLoginModel->addInfo($mobile, hash('sha256', $password), $userName, $enddate, $areacode);
        if ($result == false) {
            $commit = false;
        }
        //添加账号信息
        if ($commit) {
            $dealerInfoModel = D('DealerInfo');
            $result = $dealerInfoModel->addInfo($userId, $idcard ,$idcardpath);
            if ($result == false) {
                $commit = false;
            }
        }
        if ($commit) {
            $dealerApplyModel->commit();
            //发送未注册短信通知
            if ($areacode = '86') {
                $sms = new \Org\Util\Sms();
                $sms->sendDealerPass($mobile, $password);
            } else {
                $sms = new \Org\Util\yunPian();
                $sms->sendDealerPass($areacode, $mobile, $password);
            }
            
        } else {
            $dealerApplyModel->rollback();
            returnJson(403, 123, '开通服务失败');
        }
    }

    /**
     * 获取区号时区数据
     * @modify wenhui 2017-06-16
     * @return array $data 时区数据
     */
    public function universalTimeList() {

        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_PUBLIC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => 'uclbrt',
            'time' => time(),
        );

        $result = $client->getAreaCode($RPCData);
        if (!is_array($result)) {
            return array(
                array(
                    'fullName' => '中国',
                    'firstName' => 'C',
                    'areaCode' => '86',
                    'universalTimeId' => '5',
                    'universalTimeUtc' => 'UTC/GMT+8',
                    'universalTimeName' => '中国/China',
                    'universalTimeTimezone' => 'GMT+8',
                ),
            );
        } else {
            return $result['cn'];
        }
    }

}
